//
//  RW_UserSingleView.m
//  SleepApp
//
//  Created by chuangqi on 2021/3/30.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserSingleView.h"
#import "RW_LoginViewController.h"
#import "RW_UserSingleListViewController.h"
#import "RW_UserSingleItemViewCell.h"
#import "RW_UserSingleAddView.h"

@interface RW_UserSingleView (){
    BOOL _isEdit;
}
@property (nonatomic,strong) WLCollectionView *wlclass_collection_view;
@end
@implementation RW_UserSingleView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = HexColor(@"f6f6f6");
        [self addSubview:self.wlclass_collection_view];
        [self.wlclass_collection_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
    }
    return self;
}
#pragma mark -- 数据绑定
/** 初始化数据 */
-(void)initData{
    if (![UserModel_WL isLogin]){
        [self reloadSection:@[]];
        return;
    }
    [self.wlclass_view_model getViewSuccess:^(NSArray<WLBaseCollectionViewLayerSection *> *response) {
        [self reloadSection:response];
    } fail:^(NSError *error) {
        [self reloadSection:@[]];
    }];
}
-(void)reloadSection:(NSArray<WLBaseCollectionViewLayerSection *> *)response{
    for (WLBaseCollectionViewLayerSection *section in response) {
        for (WLBaseCollectionViewLayerRow *row in section.item) {
            if ([row.data isKindOfClass:RW_UserSingleItemViewCellModel.class]){
                if ([(RW_UserSingleItemViewCellModel *)row.data item_type] == UserSingle_Item_Type_UserSingle){
                    [(RW_UserSingleItemViewCellModel *)row.data setIsEdit:_isEdit];
                }
            }
        }
    }
    [self.wlclass_collection_view setAllSection:response];
}
-(void)initReloadData{
    [self initData];
}

#pragma mark -- 功能
/** 修改当前编辑状态 */
-(void)songEditStatus:(BOOL)isEdit{
    _isEdit = isEdit;
    [self reloadSection:[self.wlclass_collection_view getAllSection]];
}
/** 添加歌单 */
-(void)click_add_single{
    __WEAK_SELF_YLSLIDE
    RW_UserSingleAddView.init_wl()
    .maskAlpha_wl(.5)
//    .rootViewController_wl([self viewController_wl])
    .closeBlock_wl(^(id data){
        if (data){
            [weakSelf initReloadData];
        }
    }).show_wl();
}
#pragma mark -- WLUserSingleView 代理
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)layerModel
             layerRowData:(id)rowData
             didIndexPath:(NSIndexPath *)indexPath{
    if ([rowData isKindOfClass:RW_UserSingleItemViewCellModel.class]){
        [RW_UserSingleListViewController pushUserSingleSongList:[rowData user_single_model] fromVc:[self viewController_wl]];
    }
}
// 自定义显示视图
- (UIView *)wl_customViewForWLEmptyDataSet{
    if (![UserModel_WL isLogin]){
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:@"去登陆" forState:UIControlStateNormal];
        [button setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(click_go_login:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    }
    if ([self.wlclass_collection_view initReload]){
        UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [activityIndicator startAnimating];
        return activityIndicator;
    }
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:@"新建歌单" forState:UIControlStateNormal];
    [button setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(click_add_single) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/** 强制显示空数据集：当项目数量大于0时，请求代理是否仍应显示空数据集。（默认值为NO） */
- (BOOL)wl_emptyDataSetShouldBeForcedToDisplay{
    return ![UserModel_WL isLogin];
}

-(void)click_go_login:(UIButton *)sender{
    [RW_LoginViewController pushLoginFromVc:[self viewController_wl]];
}

/** 刷新列表数据 */
-(void)reloadList{
    [self initData];
}
#pragma mark -- 懒加载
-(RW_UserSingleViewModel *)wlclass_view_model{
    if (!_wlclass_view_model){
        _wlclass_view_model = [[RW_UserSingleViewModel alloc]init];
    }
    return _wlclass_view_model;
}
-(WLCollectionView *)wlclass_collection_view{
    if (!_wlclass_collection_view){
        _wlclass_collection_view = [WLCollectionView initWithVerticalDelegate:self];
    }
    return _wlclass_collection_view;
}

@end
